package com.mactivity.helper;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.mactivity.database.CustomHttpClient;

/**
 * This class extends the java mail class, sends mail to the specified user with
 * the given body content. The mail is sent from a gmail server. A separate
 * account is created for the sending the mail and the configurations are hard
 * coded in the script.
 * 
 * @author Shwetha Narayan
 * 
 */
public class MailHelper {

	/**
	 * Calls the php script which sends the mail.
	 * 
	 * @param to
	 *            String contains the emailId to whom the mail is to be sent
	 * @param emailBody
	 *            String contains the email message to be sent.
	 * @param emailSubject
	 *            String subject of the mail body to be sent.
	 * @return boolean true is the mail is sent, else returns false.
	 */
	public boolean sendMail(String to, String emailBody, String emailSubject) {

		boolean mailSent = false;

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		// else insert a new record
		postParameters.add(new BasicNameValuePair("to", to));
		postParameters.add(new BasicNameValuePair("emailBody", emailBody));
		postParameters
				.add(new BasicNameValuePair("emailSubject", emailSubject));

		try {
			String result = CustomHttpClient
					.executeHttpPost(
							"http://omega.uta.edu/~hbs1966/db_files/Mail/registration_mail_script.php",
							postParameters);
			result = result.replaceAll("\\s+", "");

			if (result.equals("1"))
				mailSent = true;

		} catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,
					"Error in http connection in Registration" + e.toString());
		}

		return mailSent;

	}

}
